package com.kotlin.mvpdemo.ui.users;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.kotlin.mvpdemo.R;
import com.kotlin.mvpdemo.base.BaseFragment;
import com.kotlin.mvpdemo.databinding.FragmentUsersBinding;
import com.kotlin.mvpdemo.ui.users.adapter.UserFragmentAdapter;
import com.kotlin.mvpdemo.ui.users.presenter.UsersPresenter;
import com.kotlin.mvpdemo.ui.users.view.UserView;

/**
 * author : fang.zhang
 * e-mail : initzf@126.com
 * date   : 2020/6/6
 * desc   :
 */
public class UsersFragment extends BaseFragment<UserView, UsersPresenter, FragmentUsersBinding> implements UserView {

    private TabLayoutMediator mLayoutMediator;
    private UserFragmentAdapter mAdapter;

    @Override
    protected UsersPresenter getPresenter() {
        return new UsersPresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_users;
    }

    @Override
    protected FragmentUsersBinding getViewBind(LayoutInflater inflater, ViewGroup container) {
        return FragmentUsersBinding.inflate(inflater, container, false);
    }

    public static UsersFragment newInstance() {

        Bundle args = new Bundle();

        UsersFragment fragment = new UsersFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ViewPager2 mViewPage2 = mViewBinding.mViewPage2;
        mViewBinding.mTabLayout.setSelectedTabIndicatorHeight(0);
        mViewPage2.setAdapter(mAdapter = new UserFragmentAdapter(getChildFragmentManager(), getLifecycle()));
        mLayoutMediator = new TabLayoutMediator(mViewBinding.mTabLayout, mViewPage2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(mAdapter.getTitle(position));
            }
        });
        mLayoutMediator.attach();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mLayoutMediator.detach();
    }
}
